//删除按钮
function delete_prodduct() {
    let $ul = $('.input ul')
    $ul.click(function (e) {
        let count = $(e.target).attr("count")
        let id = $(e.target).attr("data-id")
        if (count || count == 0) {
            let $li = $('.input ul li')
            $($li[count]).remove()
            console.log($ul.css("height"));
        }
    })
}
//渲染搜索框数据
function loding_searchData(list) {
    let $ul = $('.input ul')
    let str = ""
    for (let i = 0; i < list.length; i++) {
        str += `<li>
        <a data-id=${list[i].id}  href="./product_details.html?id=${list[i].id}" class="clearfix">
            <img src=${list[i].picture} alt="">
            <p>${list[i].product}</p>
            <div>${list[i].price}</div>
            <button count='${i}'>×</button>
        </a>
    </li>`
    }
    $ul.html(str)
    $ul.css({
        height: `${50*list.length}px`
    })
    delete_prodduct()
}
//获取搜索框数据
function ajax_getsearch(keyword, pageSize, pageNo) {
    promise({
        url: `http://api.yuguoxy.com/api/shop/search`,
        method: "get",
        data: {
            keyword,
            pageSize,
            pageNo
        }
    }).then(data => {
        console.log(data);
        if (data.resultCode == 1)
            loding_searchData(data.resultInfo.list)
    }, err => {
        alert("加载数据出错")
    })
}
//获取用户输的信息
function input_search() {
    let input = document.querySelector(".input_search")
    input.addEventListener("input", function (e) {
        if (e.target.value != "") {
            ajax_getsearch(e.target.value, 8, 1)
        } else {
            $('.input ul').css({
                height: "0"
            })
        }
    })
    input.addEventListener("blur", function () {
            $('.input ul').css({
                height: "0"
            })
    })
    input.addEventListener("focus", function (e) {
        if (e.target.value != "") {
            ajax_getsearch(e.target.value, 8, 1)
        } else {
            $('.input ul').css({
                height: "0"
            })
        }
})
}

function go_top(){
    $('.go_top').click(function(){
        let a=scrollY
        let time = setInterval(()=>{
            a-=100
            scrollTo(0,a)
            if(a<=0){
                clearInterval(time)
            }
        },10)
    })
}
//点击搜索
function button_search(){
    $(".button_search").click(function(e){
        location.href=`./search_product.html?keyword=${$(".input_search").val()}`   
    })
}
button_search()

input_search()